package com.epam.cdp.selenium2.core.runner;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.testng.TestNG;
import org.testng.xml.Parser;
import org.testng.xml.XmlSuite;

import com.epam.cdp.selenium2.core.results.logger.Logger;

public class TestRunner
{

  protected TestNG testNG = new TestNG();

  protected static CLIparser cli = new CLIparser();

  protected static CmdLineParser parser = new CmdLineParser( cli );

  public TestRunner( String[] args )
  {

  }

  public static void main( String[] args ) throws CmdLineException
  {
    parser.parseArgument( args );
    try
    {
      TestRunner tr = new TestRunner( args );
      tr.run();
    }
    catch( Exception e )
    {
      Logger.fatal( "", e );
      throw new RuntimeException( e );
    }
    finally
    {
      Logger.shutdown();
    }
  }

  public void run()
  {
    try
    {
      for( XmlSuite xmlSuite : new Parser().parseToList() )
      {
        testNG.setCommandLineSuite( xmlSuite );
      }
      testNG.run();

    }
    catch( Exception ex )
    {
      throw new RuntimeException( ex );
    }
  }
}
